package v3.chapter2.synBlockString;
 
public class Run {

	public static void main(String[] args) { 
		Service service=new Service();
		MyThread1 a=new MyThread1(service);
		a.setName("A");
		a.start();
		
		MyThread2 b=new MyThread2(service);
		b.setName("B");
		b.start();
	}

}


class MyThread1 extends Thread {

	private Service service;

	public MyThread1(Service service) {
		super();
		this.service = service;
	}

	@Override
	public void run() {
		service.setUsernamePassword("b", "bb");
		
	}

}

class MyThread2 extends Thread {

	private Service service;

	public MyThread2(Service service) {
		super();
		this.service = service;
	}

	@Override
	public void run() {
		service.setUsernamePassword("a", "aa");
		
	}

} 

class Service{
	private String usernameParam;
	private String passwordParam;
	//private String anyString=new String();
	
	public void setUsernamePassword(String username,String password) {
		try {
			String anyString=new String();
			synchronized(anyString) {
				System.out.println("线程名称为："+Thread.currentThread().getName()+" 在"+System.currentTimeMillis()+" 进入同步块");
				usernameParam=username;
				Thread.sleep(3000);
				System.out.println("线程名称为："+Thread.currentThread().getName()+" 在"+System.currentTimeMillis()+" 离开同步块");
			}
		}catch(InterruptedException e) {
			e.printStackTrace();
		}
	}
}